import { createRouter, createWebHashHistory} from 'vue-router'
import vuex from '../store/index'
import routes from './router.config'
import axios from 'axios'
function getWho() {
    return axios.get('/api/who')
}
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    if(to.path=='/login') return next()
    if(vuex.state.who.id) return next()
    getWho().then(v=>{
        console.log(v)
        if(!v.data.errcode){
            vuex.commit('setWho',v.data.data)
        }else{
            next('/login')
            return 
        }
    })
    next()
})





export default router